package com.zijin.utils;

import java.security.MessageDigest;
/**
 * @Title: aaa
 * @Author JWei Xu
 * @Package com.zijin.utils
 * @Date 2024/12/7 0:02
 * @description:
 */


public class MD5Util {

    // MD5 加密方法
    public static String md5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5"); // 创建 MD5 实例
            byte[] messageDigest = md.digest(input.getBytes()); // 获取加密后的字节数组
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b); // 转换为十六进制字符串
                if (hex.length() == 1) {hexString.append('0');} // 补零
                hexString.append(hex);
            }
            return hexString.toString(); // 返回十六进制字符串
        } catch (Exception e) {
            throw new RuntimeException("MD5 encryption error", e); // 抛出运行时异常
        }
    }
}